﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：选择报销单
 */
var dfopid = request('dfopid');
var selectValue = request('selectValue');
var selectedRow;
var refreshGirdData;
var acceptClick;

var companyId = request("companyId");
var schemeCode = request("schemeCode");
var keyword = request("keyword");

var bootstrap = function ($, learun) {
    "use strict";
    var categoryId = '1';
    var logbegin = '';
    var logend = '';

    var page = {
        init: function () {
            page.bind();
            page.initGrid();
            page.search({keyword: keyword});
        },
        bind: function () {
            $('#datesearch').lrdate({
                dfdata: [
                    {
                        name: '今天', begin: function () {
                            return learun.getDate('yyyy-MM-dd 00:00:00')
                        }, end: function () {
                            return learun.getDate('yyyy-MM-dd 23:59:59')
                        }
                    },
                    {
                        name: '近7天', begin: function () {
                            return learun.getDate('yyyy-MM-dd 00:00:00', 'd', -6)
                        }, end: function () {
                            return learun.getDate('yyyy-MM-dd 23:59:59')
                        }
                    },
                    {
                        name: '近1个月', begin: function () {
                            return learun.getDate('yyyy-MM-dd 00:00:00', 'm', -1)
                        }, end: function () {
                            return learun.getDate('yyyy-MM-dd 23:59:59')
                        }
                    },
                    {
                        name: '近3个月', begin: function () {
                            return learun.getDate('yyyy-MM-dd 00:00:00', 'm', -3)
                        }, end: function () {
                            return learun.getDate('yyyy-MM-dd 23:59:59')
                        }
                    },
                ],
                // 月
                mShow: false,
                premShow: false,
                // 季度
                jShow: false,
                prejShow: false,
                // 年
                ysShow: false,
                yxShow: false,
                preyShow: false,
                yShow: false,
                // 默认
                dfvalue: '2',
                selectfn: function (begin, end) {
                    logbegin = begin;
                    logend = end;

                    page.search();
                }
            });

            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            $("#txt_Keyword").keypress(function (even) {
                if (even.which == 13) {
                    var keyword = $(this).val();
                    page.search({keyword: keyword});
                }
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'wftasks?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "流程单号", name: "code", width: 120, align: "center"},
                    {label: "标题", name: "processName", width: 220, align: "left"},
                    {label: "模板名称", name: "schemeName", width: 150, align: "left"},

                    {
                        label: "等级", name: "processLevel", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            switch (cellvalue) {
                                case 0:
                                    return '普通';
                                    break;
                                case 1:
                                    return '重要';
                                    break;
                                case 2:
                                    return '紧急';
                                    break;
                            }
                        }
                    },
                    {
                        label: "状态", name: "status", width: 70, align: "center",
                        formatter: function (cellvalue, row) {
                            if (row.isFinished == 0) {
                                if (cellvalue == 1) {
                                    return "<span class=\"label label-success\">运行中</span>";
                                } else if (cellvalue == -1) {
                                    return "<span class=\"label label-default\">草稿</span>";
                                }
                            } else {
                                return "<span class=\"label label-warning\">结束</span>";
                            }

                        }
                    },
                    {label: "发起者", name: "createUsername", width: 70, align: "center"},
                    {
                        label: "发起时间", name: "createTime", width: 150, align: "left",
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {label: "备注", name: "description", width: 300, align: "left"},
                    {label: "id", name: "id", width: 1, align: "left", hidden: true}
                ],
                dblclick: function (row) {
                    parent.$(".layui-layer-btn0")[0].click();

                    //关闭layer弹出层
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                },
                isPage: true,
                reloadSelected: true,
                isTree: false,
                mainId: 'id'
            });
        },
        search: function (param) {
            param = param || {};
            param.startTime = logbegin;
            param.endTime = logend;
            param.categoryId = categoryId;
            param.isFinished = 1;
            if (!!schemeCode) {
                param.schemeCode = schemeCode;
            }

            $('#gridtable').jfGridSet('reload', param);
        }
    };
    refreshGirdData = function () {
        page.search();
    };

    // 保存数据
    acceptClick = function (callBack) {
        var row = $('#gridtable').jfGridGet('rowdata');
        callBack(row.id, dfopid);
        return true;
    };

    page.init();
}